package com.banyou.banyou.order.evaluatehistory;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.banyou.banyou.R;
import com.banyou.banyou.adapter.EvaluateHistoryAdapter;
import com.banyou.banyou.base.BaseActivity;
import com.banyou.banyou.bean.order.EvaluateHistoryBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * 类描述：
 * 创建人：Random
 * 修改时间：
 * 修改备注：
 */

public class EvaluateHistoryActivity extends BaseActivity<EvaluateHistoryPresenter> implements EvaluateHistoryContract.View {

    @BindView(R.id.back_btn)
    ImageView mBackBtn;
    @BindView(R.id.rv)
    RecyclerView mRv;

    private EvaluateHistoryAdapter mAdapter;
    private List<EvaluateHistoryBean> mList;
    private int mStart=0,mLimit=15,mTotal;
    private Map<String,String> mMap;
    @Override
    protected EvaluateHistoryPresenter initPresenter() {
        return new EvaluateHistoryPresenter();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_evalute_history;
    }

    @Override
    protected View initLayoutView() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter.initPresenter(this, new EvaluateHistoryModel());
        ButterKnife.bind(this);
        mList = new ArrayList<>();
        mAdapter = new EvaluateHistoryAdapter(this, mList);
        LinearLayoutManager manager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        mRv.setLayoutManager(manager);
        mRv.setAdapter(mAdapter);
        initListener();
        mMap = new HashMap<>();
        mMap.put("userType", getSharedPreference().getString("userType", ""));
        mMap.put("orderId", getIntent().getLongExtra("orderId", 0) + "");
        mMap.put("start",mStart+"");
        mMap.put("limit",mLimit+"");
        mPresenter.getData(mMap);

    }

    /**
     * 初始化监听器
     */
    private void initListener() {
        mRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //屏幕中最后一个可见子项的position
                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                //当前屏幕所看到的子项个数
                int visibleItemCount = layoutManager.getChildCount();
                //当前RecyclerView的所有子项个数
                int totalItemCount = layoutManager.getItemCount();
                if (visibleItemCount > 0 && lastVisibleItemPosition == totalItemCount - 1 && newState == RecyclerView.SCROLL_STATE_IDLE && mLimit * (mStart + 1) <= mTotal) {
                    mMap.put("start", "" + mStart++);
                    mPresenter.getData(mMap);
                }
            }
        });
    }

    @Override
    protected String pageName() {
        return "评论历史";
    }


    @OnClick(R.id.back_btn)
    public void onClick() {
        this.finish();
    }

    /**
     * 加载数据成功
     * @param beans
     */
    @Override
    public void getDataSuccess(List<EvaluateHistoryBean> beans) {
        mList.clear();
        mTotal=beans.get(0).getTotal();
        beans.remove(0);
        mList.addAll(beans);
        mAdapter.notifyDataSetChanged();
    }
}
